package com.qf.controller;

import com.alibaba.fastjson.JSONObject;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.enums.scope.AuthGiteeScope;
import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.request.AuthGiteeRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthScopeUtils;
import me.zhyd.oauth.utils.AuthStateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * http://localhost:8443/oauth/gitee
 */
@Controller
@RequestMapping("/oauth")
public class AuthController {

    @Value("${server.port}")
    private Integer port;

    @RequestMapping("/gitee")
    @ResponseBody
    public void renderAuth(HttpServletResponse response) throws IOException {
        AuthRequest authRequest =  new AuthGiteeRequest(AuthConfig.builder()
                .clientId("5eb331b8bbb1aff17a7b36abd1339cf24c2ee6345b6751dc550e7d0e18eb442b")
                .clientSecret("e0d23ac38dac1f6f6a3a832ce340a5cac8fb815dda32ed84a4229273e0831bc6")
                .redirectUri("http://127.0.0.1:"+port+"/oauth/callback/gitee")
                .scopes(AuthScopeUtils.getScopes(AuthGiteeScope.USER_INFO))
                .build());
        String authorizeUrl = authRequest.authorize(AuthStateUtils.createState());
        System.out.println(authorizeUrl);
        response.sendRedirect(authorizeUrl);
    }

    @RequestMapping("/callback/gitee")
    public ModelAndView login(AuthCallback callback, HttpServletRequest request) {
        AuthRequest authRequest = new AuthGiteeRequest(AuthConfig.builder()
                .clientId("5eb331b8bbb1aff17a7b36abd1339cf24c2ee6345b6751dc550e7d0e18eb442b")
                .clientSecret("e0d23ac38dac1f6f6a3a832ce340a5cac8fb815dda32ed84a4229273e0831bc6")
                .redirectUri("http://127.0.0.1:"+port+"/oauth/callback/gitee")
                .scopes(AuthScopeUtils.getScopes(AuthGiteeScope.USER_INFO))
                .build());
        AuthResponse<AuthUser> response = authRequest.login(callback);
        System.out.println(JSONObject.toJSONString(response));

        if (response.ok()) {
            // 保存用户的登录状态，单节点到session中，微服务到 redis中

            // 根据需求考虑是否进行用户信息持久化到关系数据库中
            // 如果需要把 三方用户信息保存到本地数据库，则进行持久化，注意每个用户名只需要进行一次即可

            // 创建jwttoken
            String jwt = "123456";

            // 重定向到前端的index.html，并且携带token
            return new ModelAndView("redirect:http://www.baidu.com?token="+jwt);
//            return new ModelAndView("redirect:http://127.0.0.1:5500/login.html?token="+jwt);
        }

        Map<String, Object> map = new HashMap<>(1);
        map.put("errorMsg", response.getMsg());

        return new ModelAndView("error", map);
    }

}
